package com.student.service;

import com.student.bean.Admin;
import com.student.bean.Student;
import com.student.dao.AdminDAOImpl;
import com.student.dao.util.JDBCUtils;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * @ClassName AdminService
 * @Description TODO
 * @Author wk
 * @Date 2022/3/3 17:00
 * @Version 1.0
 */
public class AdminService {
   private AdminDAOImpl adminDAO = new AdminDAOImpl();

    /**
     * @author wk
     * @Description 注册功能
     * @Date 17:01 2022/3/3
     * @Param
     * @Return
     */

    public int register(Admin admin) {
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            return adminDAO.register(connection, admin);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return 0;
    }

    /**
     * @author wk
     * @Description 登录功能
     * @Date 17:04 2022/3/3
     * @Param
     * @Return
     */

    public Admin login(String adminId, String password) {
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            return adminDAO.login(connection, adminId, password);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return null;
    }


    /**
     * @author wk
     * @Description 找回密码
     * @Date 19:49 2022/3/3
     * @Param
     * @Return
     */

    public String recover(String card, String phone) {
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            return adminDAO.recover(connection, card, phone);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return null;
    }

    /**
     * @author wk
     * @Description 注销账户
     * @Date 21:03 2022/3/4
     * @Param
     * @Return
     */

    public int unsubscribe(String card, String phone) {
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            return adminDAO.unsubscribe(connection, card, phone);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return 0;
    }

    /**
     * 通过账号id，检查管理员是否存在
     *
     * @param id id
     * @return boolean
     */
    public boolean checkAdminIsExistById(String id){
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            return adminDAO.checkAdminIsExistById(connection,id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return false;
    }

    /**
     * 通过身份证号，检查管理员是否存在
     *
     * @param card 身份证号
     * @return boolean
     */
    public boolean checkAdminIsExistByCard(String card){
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            return adminDAO.checkAdminIsExistByCard(connection,card);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return false;
    }

    /**
     * 通过手机号，检查管理员是否存在
     *
     * @param phone 手机号
     * @return boolean
     */
    public boolean checkAdminIsExistByPhone(String phone){
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            return adminDAO.checkAdminIsExistByPhone(connection,phone);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return false;
    }

    /**
     * @author wk
     * @Description 通过身份证号，获取管理员信息
     * @Date 20:44 2022/3/4
     * @Param
     * @Return
     */

    public Admin getAdminByCard(String card) {
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            return adminDAO.getAdminByCard(connection, card);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return null;
    }

    /**
     * @author wk
     * @Description 根据手机号，获取管理员信息
     * @Date 20:56 2022/3/4
     * @Param
     * @Return
     */

    public Admin getAdminByPhone(String phone) {
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            return adminDAO.getAdminByPhone(connection, phone);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return null;
    }
}
